package org.itboys.mobile.controller.mobile;

import com.wordnik.swagger.annotations.ApiOperation;
import com.wordnik.swagger.annotations.ApiParam;
import org.itboys.commons.CommonConstants;
import org.itboys.framework.spring.controller.BaseController;
import org.itboys.mobile.dto.TianJieGroupRecordDto;
import org.itboys.mobile.entity.mysql.TianJieGroup;
import org.itboys.mobile.entity.mysql.TianJieGroupRecord;
import org.itboys.mobile.service.mysql.mobile.TianJieMobileGroupService;
import org.itboys.mobile.service.mysql.mobile.TianJieMobileWalletBlockService;
import org.itboys.mobile.service.mysql.mobile.TianJieMobileWalletService;
import org.itboys.mobile.service.mysql.mobile.TianJieMobileWalletUtilService;
import org.itboys.mobile.util.AccountIdHoldUtils;
import org.itboys.mobile.util.ResponseMessage;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 作者：江雄
 * 日期：2017年5月10日
 * 描述：天街_拨付_controller_mobile
 */
@Controller
@RequestMapping("/tianjie/mobile/finance/present")
public class TianJieMobilePresentController extends BaseController{
    @Autowired
    private TianJieMobileGroupService groupService;

    @Autowired
    private TianJieMobileWalletUtilService walletUtilService ;

    @RequestMapping(value="/getWallet",method= {RequestMethod.POST,RequestMethod.GET})
    @ApiOperation(value="【拨付】获取拨付金额钱包。只对团队队长有效")
    public void getWallet(HttpServletRequest request,
                           HttpServletResponse response,
                           @ApiParam(value = "随机数")@RequestParam(value = "nonce", required = true) String nonce,
                           @ApiParam(value = "签名")@RequestParam(value = "sign", required = true) String sign
    ) {
        try{
            //校验签名
            Long memberId = AccountIdHoldUtils.getMemberId();
          //  Map<String,Object> result = groupService.getWallet(memberId);
           /* result.put("totalAmount",new BigDecimal("0.00"));
            result.put("ableAmount",new BigDecimal("0.00"));
            result.put("info","字段说明：totalAmount：总拨付金额（暂不使用），ableAmount：可用拨付金额");*/
           // ResponseMessage.success(result);
            ResponseMessage.error(-1,"此业务已暂停，请联系万品天街");
        }catch (Exception e){
            String msg =CommonConstants.ERROMSG;
            Integer code=CommonConstants.FAIL_CODE;
            //自定义异常（签名错误）
            if(e.getClass().getSimpleName().equalsIgnoreCase("ErrorException")){
                msg=e.getMessage();
                //token已过期
            }else  if(e.getClass().getSimpleName().equalsIgnoreCase("TokenException")){
                code=CommonConstants.TOKEN_CODE;
                msg=CommonConstants.TOKENMSG;
            }else{
                logger.error("/tianjie/mobile/finance/present/----e="+e.getMessage());
            }
            e.printStackTrace();
            ResponseMessage.error(code,msg);
        }
    }
    @RequestMapping(value="",method= {RequestMethod.POST,RequestMethod.GET})
    @ApiOperation(value="【拨付】队长拨付给团队中任何人，包括自己。备注：1，此功能只对团队队长开启，2，可拨付金额取值字段：拨付-钱包-ableAmount")
    public void present(HttpServletRequest request,
                        HttpServletResponse response,
                        @ApiParam(required = true, name = "partnerId", value = "队员ID") @RequestParam(value = "partnerId", required = true) Long partnerId,
                        @ApiParam(required = true, name = "amount", value = "拨付金额") @RequestParam(value = "amount", required = true) BigDecimal amount,
                        @ApiParam(value = "随机数")@RequestParam(value = "nonce", required = true) String nonce,
                        @ApiParam(value = "签名")@RequestParam(value = "sign", required = true) String sign
    ) {
        try{
            //校验签名
            Long memberId = AccountIdHoldUtils.getMemberId();

          //  walletUtilService.present(memberId,partnerId,amount);
           // ResponseMessage.success();
            ResponseMessage.error(-1,"此业务已暂停，请联系万品天街");
        }catch (Exception e){
            String msg =CommonConstants.ERROMSG;
            Integer code=CommonConstants.FAIL_CODE;
            //自定义异常（签名错误）
            if(e.getClass().getSimpleName().equalsIgnoreCase("ErrorException")){
                msg=e.getMessage();
                //token已过期
            }else  if(e.getClass().getSimpleName().equalsIgnoreCase("TokenException")){
                code=CommonConstants.TOKEN_CODE;
                msg=CommonConstants.TOKENMSG;
            }else{
                logger.error("/tianjie/mobile/finance/present----e="+e.getMessage());
            }
            e.printStackTrace();
            ResponseMessage.error(code,msg);
        }
    }
    @RequestMapping(value="/getRecord",method= {RequestMethod.POST,RequestMethod.GET})
    @ApiOperation(value="【拨付】获取拨付记录。")
    public void getRecord(HttpServletRequest request,
                          HttpServletResponse response,
                          @ApiParam(required = true, name = "pageSize", value = "分页参数。每页数量。默认20") @RequestParam(value = "pageSize", required = true) String pageSize,
                          @ApiParam(required = true, name = "pageNumber", value = "分页参数。第几页。默认1") @RequestParam(value = "pageNumber", required = true) String pageNumber,
                          @ApiParam(required = false, name = "sort", value = "排序参数。字段名称。默认为空") @RequestParam(value = "sort", required = false) String sort,
                          @ApiParam(required = false, name = "order", value = "排序参数。。默认为空。ASC正序/DESC倒序") @RequestParam(value = "order", required = false) String order,
                          @ApiParam(value = "随机数")@RequestParam(value = "nonce", required = true) String nonce,
                          @ApiParam(value = "签名")@RequestParam(value = "sign", required = true) String sign
    ) {
        try{
            //校验签名
            Long memberId = AccountIdHoldUtils.getMemberId();

            List<TianJieGroupRecordDto> records = groupService.getRecord(request,memberId);
            ResponseMessage.success(records);

        }catch (Exception e){
            String msg =CommonConstants.ERROMSG;
            Integer code=CommonConstants.FAIL_CODE;
            //自定义异常（签名错误）
            if(e.getClass().getSimpleName().equalsIgnoreCase("ErrorException")){
                msg=e.getMessage();
                //token已过期
            }else  if(e.getClass().getSimpleName().equalsIgnoreCase("TokenException")){
                code=CommonConstants.TOKEN_CODE;
                msg=CommonConstants.TOKENMSG;
            }else{
                logger.error("/tianjie/finance/mobile/wallet/present/getRecord----e="+e.getMessage());
            }
            e.printStackTrace();
            ResponseMessage.error(code,msg);
        }
    }

}
